package com.zzld.modules.autoinn.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.ProductPath;
import com.zzld.modules.autoinn.service.IPathService;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.TreeGrid;

@Controller
@RequestMapping("/autoinn/path")
public class PathController {

	@Autowired
	private IPathService pathservice;

	private String listurl = "/modules/autoinn/path/pathList";
	private String cruurl = "modules/autoinn/path/pathCru";

	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model, HttpServletRequest request) {
		return listurl;
	}

	@RequestMapping(value = "update/pro")
	public String Cru(Model model, HttpServletRequest request) {
		return cruurl;
	}

	@RequestMapping(value = "update/pro/json", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON updateorinser(Model model, HttpServletRequest request,
			ProductPath path) {
		CommonJSON json = new CommonJSON();
		if (StringUtils.isBlank(path.getId())) {// 新增
			path.setId(UuidUtil.uuid().toString());
			path.setCreate_date(new Date());
			try {
				if (pathservice.save(path) > 0) {
					json.setMessage(MessageUtils.ADD_SUCCESS);
					json.setSuccess(true);
				} else {
					json.setMessage(MessageUtils.ADD_FAIL);
					json.setSuccess(false);
				}
			} catch (Exception e) {
				e.printStackTrace();
				json.setMessage(MessageUtils.ADD_FAIL);
				json.setSuccess(false);
			}
		} else {
			try {
				if (pathservice.updateByPrimaryKeySelective(path) > 0) {
					json.setMessage(MessageUtils.EDIT_SUCCESS);
					json.setSuccess(true);
				} else {
					json.setMessage(MessageUtils.EDIT_WARING);
					json.setSuccess(false);
				}
			} catch (Exception e) {
				e.printStackTrace();
				json.setMessage(MessageUtils.EDIT_WARING);
				json.setSuccess(false);
			}
		}
		return json;

	}

	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON del(Model model, HttpServletRequest request,
			@PathVariable("id") String id) {
		CommonJSON json = new CommonJSON();
		if (pathservice.selectbypid(id).size()>0) {
			json.setMessage("请先删除子节点");
			json.setSuccess(false);
		} else {
			try {
				pathservice.deleteByPrimaryKey(id);
				json.setMessage(MessageUtils.DELETE_SUCCESS);
				json.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
				json.setMessage(MessageUtils.DELETE_WARNING);
				json.setSuccess(false);
			}
		}

		return json;
	}

	@RequestMapping(value="ser",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject set(Model model,HttpServletRequest request){
    	JSONObject json = new JSONObject();
    	
    	try{
    		Map<String,Object> map = new  HashMap<String,Object>();
    		ProductPath path = pathservice.selectByPrimaryKey(request.getParameter("id"));
    		map.put("id", path.getId());
    		map.put("pathname", path.getPathname());
    		map.put("pid", path.getPid());
    		map.put("sort", path.getSort());
    		map.put("islast", path.getIslast());
    		json.putAll(map);
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    	return json;
    }

	@RequestMapping(value = "treegrid")
	@ResponseBody
	public List<TreeGrid> treegird(Model model, HttpServletRequest request) {
		List<TreeGrid> list = pathservice.treeGrid();
		return pathservice.treeGrid();
	}

}
